/**
* @file          : SL_niv.c
* @author        : mcu Lab 
* @date          : mm/dd/yyy
* @brief         : This file is interrupt function.
* @version       : Ver. 1.00

* H/W Platform   : SL_FOC FOR MOTOR CONCTROL

*------------------------------------------------------------------------------

* Compiler info  : keil v5

*------------------------------------------------------------------------------
 
* Note: In this software, the function is used in motor control.

*-------------------------------------------------------------------------------

*  History:  

*              mm/dd/yyyy ver. x.y.z author

*              mm1/dd1/yyyy1 ver. x.y.z author1

*------------------------------------------------------------------------------
* @attention
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, SLMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* THIS SOURCE CODE IS PROTECTED BY A LICENSE.
* FOR MORE INFORMATION PLEASE CAREFULLY READ THE LICENSE AGREEMENT FILE LOCATED
* IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.

* <h2><center>&copy; COPYRIGHT 2015 SLMicroelectronics</center></h2>
******************************************************************************
*/


/******************************************************************************
* include file
*******************************************************************************/
#include "SC32F58128.h"
#include "SC32F58128_Timer.h"
#include "devInit.h"
#include "BSP_adc.h"
#include "BSP_pwm.h"
#include "BSP_gpio.h"
#include "BSP_dma.h"
#include "BSP_uart.h"
#include "timer4Tick.h"

#include "api_fan_pmsm_pfc.h"
#include "api_dbg.h"

//#include "uart6_inout_comm.h"
//#include "uart0_pc_comm.h"

/******************************************************************************
* global data for the project
*******************************************************************************/
extern uint32_t  mg_u32IdlePercent0;
extern uint32_t  mg_u32IdlePercent1;
extern uint32_t  mg_u32IdleCnt0;
extern uint32_t  mg_u32IdleCnt1;


/******************************************************************************
* loacal data for the current file
*******************************************************************************/


/******************************************************************************/
/* local prototypes function **************************************************/
/******************************************************************************/

/******************************************************************************
* loacal function for the current file
*******************************************************************************/


/**
  * @brief      main function.
  * @param[in]  void : None.
  * @param[out] void : None.
  * @retval     none
  * @pre //file Var should retrun from OpenFile
  * @see //function :: OpenFile
  * @par        Example:
  * -This example shows how to call the function:
  * @code
  * main();
  * @endcode
  * @warning:
*/
/******************************************************************************/
/*            Cortex-M0 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
* @brief   This function handles NMI exception.
* @param  None
* @retval None
*/
void NMI_Handler(void)
{
}

/**
* @brief  This function handles Hard Fault exception.
* @param  None
* @retval None
*/
void HardFault_Handler(void)
{
	uint32_t u32Dlycnt;
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
    ACCESS_ENABLE;
    PWM_PMSM_CHNL0->TZFRC = 4;  // Force TZ
    PWM_PMSM_CHNL1->TZFRC = 4;  // Force TZ
    PWM_PMSM_CHNL2->TZFRC = 4;  // Force TZ
    PWM_FAN_CHNL0->TZFRC = 4;  // Force TZ
    PWM_FAN_CHNL1->TZFRC = 4;  // Force TZ
    PWM_FAN_CHNL2->TZFRC = 4;  // Force TZ
    PWM_PFC_CHNL->TZFRC = 4;  // Force TZ
    ACCESS_DISABLE;
  }
}
     
/**
* @brief  This function handles SysTick Handler.
* @param  None
* @retval None
*/
void SysTick_Handler(void)
{
	g_u32SysTickNumb++;
  if(g_u32SysTickNumb == 10000){    // 10 second
    g_u32SysTickNumb = 0;
    mg_u32IdlePercent0 = mg_u32IdleCnt0;
    mg_u32IdlePercent1 = mg_u32IdleCnt1;
    mg_u32IdleCnt0 = 0;
    mg_u32IdleCnt1 = 0;
  }
}


/**
* @brief  This function handles SysTick Handler.
* @param  None
* @retval None
*/
void SVC_Handler(void)
{

}


/**
* @brief  This function handles SysTick Handler.
* @param  None
* @retval None
*/
void PendSV_Handler(void)
{

}

/******************************************************************************/
/*                 Peripherals Interrupt Handlers                             */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file .                                               */
/******************************************************************************/
/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void UART0_RX_IRQHandler(void)
{
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void UART0_IRQHandler(void)
{
  volatile uint8_t tt;

  if(IS_INT_MASK_RX(0)){
    DBG_SetRxData(UART_BUFF);
    CLR_INT_RX(0);
  }

  if(IS_INT_MASK_TX(0)){
    CLR_INT_TX(0);
  }

  if(IS_INT_MASK_OE(0)){
    tt = UART_BUFF;
    CLR_INT_OE(0);
  }
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void UART12_IRQHandler(void)
{
//  volatile uint8_t tt;
//  uint32_t u32IntSataus;
//  uint8_t  u8tt,u8Test[8];
//  mg_u8Core0IsrCnt++;
//  TIMER1->CTRL |= 1;

//  /* UART1 interrupt */
//  if (SYSREG->INTR_STATUS0_b.UART1_INTR){
//    if(IS_RX1_INT){
//      CLR_RX1_INT();
//      uart6_receive_interrupt();
//    }

//    if(IS_TX1_INT){
//      CLR_TX1_INT();
//      uart6_send_interrupt();
//    }

//    if(IS_OVERURN1_INT){
//      tt = UART1->UARTDR_b.DATA;
//      uart6_error();
//      CLR_UART1_INT();
//    }

//    CLR_UART1_INT();
//  }

//  /* UART2 interrupt */
//  if (SYSREG->INTR_STATUS0_b.UART2_INTR){
//    if((IS_RX2_INT) || (UART2->UARTIFLS_b.RXIFLSEL)){
//      tt = 0;
//      u8tt = 0;
//      while((UART2->UARTFR_b.RXFE==0) || (UART2->UARTRIS_b.RT_I)){
//        u8Test[u8tt++] = UART2->UARTDR_b.DATA;
//        if(u8tt >= 8)  u8tt = 0;
//      }

//      r_uart2_interrupt_receive(u8Test);
//      CLR_RX2_INT();
//    }
//	  if(IS_TX2_INT){
//      CLR_TX2_INT();
//    }

//    if(IS_OVERURN2_INT){
//      tt = UART2->UARTDR_b.DATA;
//      IntUart2ReceiveError();
//      CLR_UART2_INT();
//    }

//    CLR_UART2_INT();
//  }

//  mg_u8Core0IsrCnt--;
//  if(mg_u8Core0IsrCnt == 0){
//    TIMER1->CTRL &= 0xfffffffe;
//  }
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void SSP01_IRQHandler(void)
{
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void SSP23_IIC_IRQHandler(void)
{ 
}
/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void CAN_IRQHandler(void)
{
}
 /**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void ECAP0_IRQHandler(void)
{
  ECAP0->ECCLR = 0x80;  // Clear CMP int flag
  DMA->SW_REQ = 0xe;    // Start DMA
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void ECAP1_IRQHandler(void)
{
}
/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void ECAP2_IRQHandler(void)
{
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void EQEP0_IRQHandler(void)
{
}


/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void EQEP1_IRQHandler(void)
{
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void GPIO_IRQHandler(void)
{
  /* FAN Fault */
  if(GetGpioIntFlg_FAN_FLT())
  {
    ACCESS_ENABLE;
    PWM_FAN_CHNL0->TZFRC = 4;  // Force TZ
    PWM_FAN_CHNL1->TZFRC = 4;  // Force TZ
    PWM_FAN_CHNL2->TZFRC = 4;  // Force TZ
    ACCESS_DISABLE;

    FanTzFunction();
    ClrGpioIntFlg_FAN_FLT();
  }
}

 
/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void DMA_IRQHandler(void)
{
	/**< DMA Channel 0 Done ISR */
  if(IS_DMA0_CHNL_APINT)
  {
    CLR_DMA0_INT_APFLG();
    SendNotSendData2UartTx();
  }
}


/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void COPROC_IRQHandler(void)
{
}


/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void SYS_IRQHandler(void)
{
}


/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void rsv1_IRQHandler(void)
{
}


/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void TIMER0_IRQHandler(void)
{
  PmsmSlowLoopFunction();
  PfcSlowLoopFunction();
  #if FAN_CTRL_EN
    FanSlowLoopFunction();
  #endif

  TIMER[0]->CTRL_b.OV = 0;
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void TIMER1_IRQHandler(void)
{
}



/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void TIMER234_IRQHandler(void)
{

}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void ADC1_IRQHandler(void)
{
}


/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
#pragma arm section code = "RAMCODE"
void ADC2_IRQHandler(void)
{
  uint32_t u32Tmp;

  /* Kick WDT */
  u32Tmp = WDT0->WDOGLOAD;  // Read Load Value
  WDT0->WDOGLOAD = u32Tmp;  // Kick Dog

  ADC->ADCINTFLGCLR_b.ADCINT2 = 1;
  PfcFastLoopFunction();

//  WAVE_FillWaveBuff();
}
#pragma arm section

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void ADC3_IRQHandler(void)
{
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void ADC_IRQHandler(void)
{
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
#pragma arm section code = "RAMCODE"
void PWM0_IRQHandler(void)
{
  PmsmFastLoopFunction();

  WAVE_FillWaveBuff();
  PWM_INT_CLR(0);
}
#pragma arm section

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
#pragma arm section code = "RAMCODE"
void PWM3_IRQHandler(void)
{
  FanFastLoopFunction();

//  WAVE_FillWaveBuff();
  PWM_INT_CLR(3);
}
#pragma arm section

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void PWM4_IRQHandler(void)
{
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void PWM7_IRQHandler(void)
{
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void PWM1256_IRQHandler(void)
{
//  mg_u8Core0IsrCnt++;
//  TIMER1->CTRL |= 1;

//  if(PWM_SOC_DLY_CHNL->ETFLG_b.INT){
//    PWM_SOC_DLY_CHNL->ETCLR |= 1;
//    DMA->SW_REQ = 0xe;
//  }

//  mg_u8Core0IsrCnt--;
//  if(mg_u8Core0IsrCnt == 0){
//    TIMER1->CTRL &= 0xfffffffe;
//  }
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void ERU0_IRQHandler(void)
{
}

/**
* @brief  This function handles DMA interrupt request.
* @param  None
* @retval None
*/
void ERU1_IRQHandler(void)
{
}

/**
* @brief  This function handles ERU2 interrupt request.
* @param  None
* @retval None
*/
void ERU2_IRQHandler(void)
{
}

/**
* @brief  This function handles ERU3 interrupt request.
* @param  None
* @retval None
*/
void ERU3_IRQHandler(void)
{
}





/************************ (C) COPYRIGHT SLMicroelectronics *****END OF FILE****/
